package searchGoogle;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

public class SearchGoogle {

	public static void main(String[] args) {
		List<String> availableIPs = new ArrayList<String>();
		
		/*Google IP地址列表*/
		String[][] googleAddressList = {
				{"64.233.160.0", "64.233.191.255"},
				{"66.102.0.0", "66.102.15.255"},
				{"66.249.64.0", "66.249.95.255"},
				{"72.14.192.0", "72.14.255.255"},
				{"74.125.0.0", "74.125.255.255"},
				{"209.85.128.0", "209.85.255.255"},
				{"216.239.32.0", "216.239.63.255"}
			};
		for (int i = 0; i < googleAddressList.length; i++) {
			String nextIp = googleAddressList[i][0];
			do {
//				System.out.print(nextIp + "	");
				
				System.out.print("trying to connect to " + nextIp);
				boolean isReach = SearchGoogle.isReachable(nextIp);
				if (isReach) {
					System.out.println(". YEAH!! I catch it:" + nextIp);
					availableIPs.add(nextIp);
				} else {
					System.out.println(". Sorry, I'm failed!");
				}
				
				nextIp = SearchGoogle.nextIp(nextIp, googleAddressList[i][1]);
			} while(null != nextIp);
		}
		
		System.out.println("Finally all the available ip are:");
		System.out.println(availableIPs);
	}
	
	public static String nextIp(String ip, String maxIp) {
		if (ip.equals(maxIp)) {
			return null;
		}
		
		String[] s = ip.split("\\.");
		String[] max = maxIp.split("\\.");
		
		int temp = Integer.parseInt(s[3]);
		temp = SearchGoogle.sum1(temp);
		if (temp == 0) {
			// sum higher
			s[3] = "0";
			temp = Integer.parseInt(s[2]);
			temp = SearchGoogle.sum1(temp);
			if (temp == 0) {
				// sum higher
				s[2] = "0";
				temp = Integer.parseInt(s[1]);
				temp = SearchGoogle.sum1(temp);
				if (temp == 0) {
					// sum higher
					s[1] = "0";
					temp = Integer.parseInt(s[0]);
					temp = SearchGoogle.sum1(temp);
					if (temp == 0) {
						return null;
					} else {
						// compare
						s[0] = String.valueOf(temp);
					}
				} else {
					// compare
					s[1] = String.valueOf(temp);
				}
			} else {
				// compare
				s[2] = String.valueOf(temp);
			}
		} else {
			// compare
			s[3] = String.valueOf(temp);
		}
		
		for (int i = 0; i < 4; i++) {
			int a = Integer.parseInt(s[i]);
			int b = Integer.parseInt(max[i]);
			if (a > b) {
				return null;
			}
		}

		return s[0] + "." + s[1] + "." + s[2] + "." + s[3];
	}
	
	private static int sum1(int low) {
		low++;
		if (low > 255) {
			return 0;
		} else {
			return low;
		}
	}
	
	public static boolean isReachable(String address) {
//		HttpMethod method = new GetMethod("http://www.baidu.com");
//		HttpMethod method = new GetMethod("http://180.97.33.108");
//		HttpMethod method = new GetMethod("http://www.google.com");
//		HttpMethod method = new GetMethod("http://74.125.203.113");
		HttpMethod method = new GetMethod("http://" + address);
		
		HttpClient client = new HttpClient();
		client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
		client.getHttpConnectionManager().getParams().setSoTimeout(3000);
		try {
			int status = client.executeMethod(method);
//			System.out.println(status);
			if (status == HttpStatus.SC_OK) {
				return true;
			}
		} catch (Exception e) {
//			e.printStackTrace();
		}
		return false;
	}

}
